
#include "../include/type.h"
#include "../include/server.h"

#define MAXEVENT 20
#define BUFFSIZE 100


int g_stop = 0 ;

void sign_handler(int signum)
{
        g_stop = 1 ;
}


int main(int argc,char **argv)
{

	int		                        server_port = -1;
	int		                        sockfd 	= -1;
	int		                        epollfd	= -1;
	int		                        rv 	= -1;
    int                             i;
    struct epoll_event              revent[MAXEVENT];
    u8                              receive_buff[BUFFSIZE];
    pack_t                          parse_pack;
    int                             actnum = 0;
    sqlite3                         *db;
	//参数解析
	server_port = argument_parse(argc, argv);
	if(server_port < 0)
	{
		return 0;
	}

    //创建或打开数据库
    datbas_open(&db);

	//套接字初始化
	sockfd = socket_init(server_port);
	if(sockfd < 0)
	{
		return 0;
	}

	//初始化epoll实例
	epollfd = epoll_init(sockfd);
    if(epollfd < 0)
    {
        close(sockfd);
        return 0;
    }

    printf("Waiting for client connecting...\n");

    while(!g_stop)
    {
        actnum = epoll_wait(epollfd,revent,MAXEVENT,-1);//-1:永远阻塞直到有文件描述被触发时返回
        if(actnum < 0)
        {
            printf("epoll_wait error:%s\n",strerror(errno));
            close(epollfd);
            return 0;
        }
        else
        {
            for(i = 0; i < actnum; i++)
            {
                if(revent[i].data.fd == sockfd)
                {
                    rv = connect_client(epollfd,sockfd);
                    if(rv < 0)
                    {
                        close(sockfd);
                        close(epollfd);
                        break;
                    }
                }
                else
                {
                    if(revent[i].events & EPOLLIN)
                    {
                        memset(receive_buff,0,sizeof(receive_buff));
                        if( (rv=read(revent[i].data.fd,receive_buff, sizeof(receive_buff)))<=0)
                        {
                            printf("socket[%d] read failure or get disconncet and will be removed.\n", revent[i].data.fd);
                            epoll_ctl(epollfd, EPOLL_CTL_DEL, revent[i].data.fd, NULL);
                            close(revent[i].data.fd);
                            continue;
                        }
                        else
                        {
                            printf("socket[%d] read get %d bytes data\n",revent[i].data.fd, rv);                        
                        
                            rv = tlv_unpack(receive_buff, rv, &parse_pack);
                            if(rv < 0)
                            { 
                                close(revent[i].data.fd);
                                revent[i].data.fd = -1;
                            }
                            else
                            {
                                printf("Unpack successfully!\n");
                            }

                            rv = datbas_insert(db, parse_pack.date, parse_pack.time, parse_pack.ID, parse_pack.temp);   //保存数据到数据库
                            if(rv < 0)
                            {
                                close(revent[i].data.fd);
                                revent[i].data.fd = -1;
                            }
                       
                    /*    rv = send_data(revent[i].data.fd,"The server receives data to complete!");
                        if(rv < 0)
                        {
                            close(revent[i].data.fd);
                            revent[i].data.fd = -1;
                        }
                    */


                        }
                    }
                }
            }

        }

    }
    //关闭数据库
    datbas_close(db);

	return 0;
}
